package beanstao.util.store.database.impl;

import java.util.Map.Entry;

import beanstao.util.Sber;
import beanstao.util.enums.DataType;
import beanstao.util.store.database.DbSetting;
import beanstao.util.store.database.Field;
import beanstao.util.store.database.Fields;


public class OracleDbCreator extends AbstractDbCreator
{
	@Override
	public String checkTabel(String storeName)
	{
		// select count(*) from user_TABLES where table_name = 表名（大写）；
		String checkSql = "SELECT TABLE_NAME FROM user_TABLES WHERE TABLE_NAME='" + storeName.toUpperCase() + "'";
		return checkSql;
	}

	@Override
	public String checkIndex(String storeName)
	{
		// select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and
		// t.table_name='NODE'
		String checkSql = "SELECT t.column_name FROM user_ind_columns t WHERE t.table_name='" + storeName.toUpperCase() + "'";
		return checkSql;
	}

	@Override
	public String checkFields(String storeName)
	{
		// select count(*) from User_Tab_Columns where table_name=表名（大写） and column_name=字段名（大写）
		String checkSql = "SELECT column_name FROM User_Tab_Columns WHERE TABLE_NAME='" + storeName.toUpperCase() + "'";
		// String checkSql = "DESC " + storeName.toUpperCase();
		return checkSql;
	}

	@Override
	public String createTabel(String dbname, String storeName, Fields fields, DbSetting _DbSetting)
	{
		//
		Sber sber = Sber.ME("CREATE TABLE ").append(storeName).append(" ( ");
		for (Entry<String, Field> entry : fields)
		{
			if ("id".equals(entry.getValue().getName()))
			{
				if (entry.getValue().isPrimary())
					sber.append("id VARCHAR2(64) PRIMARY KEY,");
				else
					sber.append("id VARCHAR2(64),");
				continue;
			}

			// 追加字段名
			sber.append(entry.getValue().getName()).append(" ");

			// 获取字段的数据类型&&追加字段的数据类型
			DataType type = entry.getValue().getDataType();

			sber.append(type);
			if (!(type.equals(DataType.DATE)) && !(type.equals(DataType.TIMESTAMP)) && !(type.equals(DataType.INTEGER)))
				sber.append("(").append(entry.getValue().getLength()).append(") ");
			else
				sber.append(' ');
			sber.append(entry.getValue().getNullEnable() + ",");
		}
		sber.trimEnd();
		sber.append(") tablespace " + dbname.toUpperCase());
		return sber.toString();
	}

	@Override
	public String createIndex(String storeName, String column)
	{
		String createSql = "CREATE INDEX " + column + "ON " + storeName.toUpperCase() + "(" + column + ")";
		return createSql;
	}

	@Override
	public String createFields(String storeName, Fields fields)
	{
		Sber sber = Sber.ME("ALTER TABLE ").append(storeName.toUpperCase()).append(" ADD (");
		for (Entry<String, Field> entry : fields)
		{
			sber.append(entry.getValue().getName() + ' ');
			DataType type = entry.getValue().getDataType();
			// if (type.equals(DataType.VARCHAR) || type.equals(DataType.TEXT))
			// type = DataType.VARCHAR2;
			// if (type.equals(DataType.TINYINT))
			// type = DataType.CHAR;
			// if (type.equals(DataType.INT))
			// type = DataType.INTEGER;
			// if (type.equals(DataType.DATETIME))
			// type = DataType.DATE;
			sber.append(type);
			if (!(type.equals(DataType.DATE)) && !(type.equals(DataType.TIMESTAMP)) && !(type.equals(DataType.INTEGER)))
				sber.append("(").append(entry.getValue().getLength()).append(") ");
			else
				sber.append(' ');
			sber.append(entry.getValue().getNullEnable() + ",");
		}
		sber.trimEnd();
		sber.append(" )");
		return sber.toString();
	}

	public String createtrigger(String storeName)
	{
		Sber sber = Sber.ME("CREATE OR REPLACE TRIGGER " + storeName + "_tri");
		sber.append(" BEFORE INSERT ON ");
		sber.append(storeName);
		sber.append(" FOR EACH ROW ");
		sber.append(" begin select " + storeName + "_seq.nextval into:New.id from dual; end;");
		return sber.toString();
	}

	public String createsequence(String storeName)
	{
		Sber sber = Sber.ME("CREATE SEQUENCE " + storeName + "_seq");
		sber.append(" INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE CACHE 10");
		return sber.toString();
	}
}
